package com.voice.call.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.voice.call.entity.LoginLog;
import com.voice.call.mapper.LoginLogMapper;
import com.voice.call.service.LoginLogService;
import com.voice.call.utils.DateUtil;
import com.voice.call.utils.JSONUtil;


@Service
public class LoginLogServiceImpl implements LoginLogService{
	
	private static final Logger log = LoggerFactory.getLogger(LoginLogServiceImpl.class);
	
	@Resource
	private LoginLogMapper loginLogMapper;
	
	@Override
	public boolean addLoginLogPhoneRecord(String xaid,String mac,String ip,String phone,String type){
      try {
			
			LoginLog entity = new LoginLog();
			entity.setXaid(xaid);
			entity.setMac(mac);
			entity.setIp(ip);
			entity.setPhone(phone);
			entity.setType(type);
			return this.addLoginLog(entity);
			
		} catch (Exception e) {
			e.printStackTrace();
			log.error("登录日志写入失败");
			return false;
		}
	}
	
	@Override
	public boolean addLoginLogRecord(String xaid,String mac,String ip,String type){
		
		try {
			
			LoginLog entity = new LoginLog();
			entity.setXaid(xaid);
			entity.setMac(mac);
			entity.setIp(ip);
			entity.setType(type);
			return this.addLoginLog(entity);
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
	}

	@Override
	public boolean addLoginLog(LoginLog entity) {
		
		if (entity!=null) {
			
			Date nowDate = new Date();
			
			String uuid = UUID.randomUUID().toString().replaceAll("-", "");
			entity.setUuid(uuid);
			entity.setCtime(nowDate);
			entity.setYmd(DateUtil.getDateTimeString(nowDate, DateUtil.DATE_NO_SPACE_FMT));
			log.info("登录日志={}",JSONUtil.objectToJson(entity));
			if (loginLogMapper.insert(entity)>0) {
				return true;
			}
			
		}
		
		return false;
		
	}
	@Override
	public boolean deleteYmdLog(String ymd){
		
		Map<String, Object> parmMap=new HashMap<String, Object>();
		parmMap.put("ymd", ymd);
		return loginLogMapper.deleteHistoryData(parmMap)>0?true:false;
		
	}
	
	@Override
	public int getFindByPhoneCount(String phone){
		String ymd = DateUtil.getNowDateTimeString(DateUtil.DATE_NO_SPACE_FMT);
		Map<String, Object> parmMap=new HashMap<String, Object>();
		parmMap.put("ymd", ymd);
		parmMap.put("phone", phone);
		return loginLogMapper.getLoginLogCount(parmMap);
	}
	
	@Override
	public int getFindByIpCount(String ip){
		String ymd = DateUtil.getNowDateTimeString(DateUtil.DATE_NO_SPACE_FMT);
		Map<String, Object> parmMap=new HashMap<String, Object>();
		parmMap.put("ymd", ymd);
		parmMap.put("ip", ip);
		
		return loginLogMapper.getLoginLogCount(parmMap);
	}
	
	@Override
	public int getFindByXaidMacCount(String xaid,String mac){
		String ymd = DateUtil.getNowDateTimeString(DateUtil.DATE_NO_SPACE_FMT);
		Map<String, Object> parmMap=new HashMap<String, Object>();
		parmMap.put("ymd", ymd);
		parmMap.put("xaid", xaid);
		parmMap.put("mac", mac);
		
		return loginLogMapper.getLoginLogCount(parmMap);
	}
	
	//清空历史登录数据
	@Override
	public void clearHistoryLoginData(){
		
		Date nowDate = new Date();
		String ymd = DateUtil.getDateTimeString(nowDate, DateUtil.DATE_TIME_FMT);
		//log.error("清空历史登录数据,ymd={}",ymd);
		
		Map<String, Object> parmMap=new HashMap<String, Object>();
		parmMap.put("ctime", ymd);
		
		loginLogMapper.deleteHistoryData(parmMap);
	}
}
